﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Qianke.Core.Common
{
    public static class DateTimeHelper
    {
        /// <summary>  
        /// Unix时间戳转为C#格式时间  
        /// </summary>  
        /// <param name="timeStamp">Unix时间戳格式,例如1482115779</param>  
        /// <returns>C#格式时间</returns>  
        public static DateTime GetFromTimeStamp(this string timeStamp)
        {
            return GetFromTimeStamp(timeStamp.ToInt64(0));
        }

        /// <summary>
        /// 获取时间搓
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <param name="isSoc">是否为秒数 true=是  false=为毫秒数  默认为true </param>
        /// <returns></returns>
        public static DateTime GetFromTimeStamp(this long timeStamp,bool isSoc = true)
        {
            if (isSoc)
            {
                var dateTimeUtc = DateTimeOffset.FromUnixTimeSeconds(timeStamp);
                return TimeZoneInfo.ConvertTimeFromUtc(dateTimeUtc.DateTime, TimeZoneInfo.Local);
            }
            else
            {
                var dateTimeUtc = DateTimeOffset.FromUnixTimeMilliseconds(timeStamp);
                return TimeZoneInfo.ConvertTimeFromUtc(dateTimeUtc.DateTime, TimeZoneInfo.Local);
            }
            
            
            //DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            //if (isSoc)
            //    return dtStart.Add(TimeSpan.FromSeconds(timeStamp));
            //else
            //    return dtStart.Add(TimeSpan.FromMilliseconds(timeStamp));

        }


        /// <summary>  
        /// DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time"> DateTime时间格式</param>  
        /// <returns>Unix时间戳格式</returns>  
        public static long GetTimeStamp(this DateTime time, bool isSoc = true)
        {
            var dateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse("1970-1-1"), TimeZoneInfo.Local);
            if (!isSoc)
            {
                return (long)(time - dateTime).TotalMilliseconds;
            }

            return (long)(time - dateTime).TotalSeconds;
        }

        /// <summary>
        /// 比较两个时间的差值
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="dateEnd"></param>
        /// <param name="timeType"></param>
        /// <returns></returns>
        public static int DateDiff(DateTime dateStart, DateTime dateEnd, TimeTypeEnum timeType = TimeTypeEnum.日)
        {
            //DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
            //DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
            TimeSpan sp = dateEnd.Subtract(dateStart);
            switch (timeType)
            {
                default:
                case TimeTypeEnum.年:
                case TimeTypeEnum.月:
                case TimeTypeEnum.日:
                    return sp.Days;
                case TimeTypeEnum.时:
                    return sp.Hours;
                case TimeTypeEnum.分:
                    return sp.Minutes;
                case TimeTypeEnum.秒:
                    return sp.Seconds;
            }
        }


    }

    public enum TimeTypeEnum : byte
    {
        /// <summary>
        /// 年
        /// </summary>
        [Description("年")]
        年 = 0,

        /// <summary>
        /// 月
        /// </summary>
        [Description("月")]
        月 = 1,

        /// <summary>
        /// 日
        /// </summary>
        [Description("日")]
        日 = 2,

        /// <summary>
        /// 时
        /// </summary>
        [Description("时")]
        时 = 4,

        /// <summary>
        /// 分
        /// </summary>
        [Description("分")]
        分 = 8,

        /// <summary>
        /// 秒
        /// </summary>
        [Description("秒")]
        秒 = 16,
    }
}
